import { useRef, useMemo } from "react";

/**
 * https://ahooks.js.org/zh-CN/hooks/use-memoized-fn
 * 持久化 function 的 Hook，一般情况下，可以使用 useMemoizedFn 完全代替 useCallback
 */
function useMemoizedFn(fn) {
  const fnRef = useRef(fn);
  fnRef.current = useMemo(() => fn, [fn]);
  const memoizedFn = useRef();
  if (!memoizedFn.current) {
    memoizedFn.current = function (...args) {
      return fnRef.current.apply(this, args);
    };
  }
  return memoizedFn.current;
}
export default useMemoizedFn;
